![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Save Errors in a FileInstead of presenting error messages to the user, a program can append error messages to a file. You can periodically check this file to see what errors have occurred. Program FileErr demonstrates this technique. When you click its Raise Error button, the program invokes the SaveErrorToFile subroutine as shown in the following code.
Raise an error.
Private Sub cmdRaiseError_Click()
On Error GoTo RaiseErrorError
Raise a divide by zero error.
Err.Raise 11
RaiseErrorError:
SaveErrorToFile FileError.cmdRaiseError_Click
Exit Sub
End Sub
Subroutine SaveErrorToFile begins by using the MakeErrorMessage function to create an appropriate error message. It then opens a log file, writes the error information into the file, and closes the file. In this example, the program writes to the file errors.log in the current directory. You could modify it to write to a file in a fixed location, possibly on a shared network hard drive. If SaveErrorToFile has trouble saving information to the log file, it falls back to the more elementary strategy of displaying the error to the user. It uses MakeErrorMessage to create a new message describing the problem with the error log file. It then displays both messages to the user.
Append mail to an error log file.
Public Sub SaveErrorToFile(ByVal routine_name As String)
Dim txt1 As String
Dim txt2 As String
Dim file_name As String
Dim fnum As Integer
Dim file_open As Boolean
Create the error message.
txt1 = MakeErrorMessage(routine_name)
On Error GoTo SaveErrorToFileError
Open the file.
fnum = FreeFile
file_name = App.Path
If Right$(file_name, 1) <> \ _
Then file_name = file_name & \
file_name = file_name & errors.log
Open file_name For Append As #fnum
Remember that the file is open.
file_open = True
Write the date and time, and the error message.
Print #fnum, Now
Print #fnum, txt1
Print #fnum, **********
Close the file.
Close #fnum
Exit Sub
SaveErrorToFileError:
There was an error saving to the file.
Just present the message.
txt2 = MakeErrorMessage(SaveErrorToFile)
MsgBox Error saving error message to file. & _
vbCrLf & _
Please record the following information & _
and send it to the development team: & _
vbCrLf & vbCrLf & txt2 & _
vbCrLf & vbCrLf & txt1
Close the file if we opened it.
If file_open Then Close #fnum
Exit Sub
End Sub
Make an appropriate error message.
Public Function MakeErrorMessage(ByVal routine_name As String) As String
MakeErrorMessage = _
Unexpected error & _
Format$(Err.Number) & _
in & routine_name & . & _
vbCrLf & Err.Description
End Function
When subroutine SaveErrorToFile works, it adds messages like the ones shown here to the error log file.
6/30/98 4:33:36 PM
Unexpected error 11 in ProcessRequests.PrintReports.
Division by zero
**********
6/27/98 1:57:36 PM
Unexpected error 9 in ProcessRequests.AlignArrays.
Subscript out of range
**********
When SaveErrorToFile has a problem, it displays an error message like the one shown in Figure 13.3. Save Errors in a DatabaseIf a program saves error messages in a file, you can cut and paste them into reports, error management systems, or other documentation. If the messages are saved in a database, you can also write programs to manipulate them. For example, you could develop an incident tracking system. For each incident, the database would record such items as description, status (New, Assigned, Fixed, Tested, etc.), person assigned, actions taken, date reported, date fixed, and so forth. With this system in place, a program can write error messages directly into the incident tracking database. It can automatically enter values like the description and creation date, and it can set the errors status to New. These errors will automatically appear in new incident reports. Program DBErr, available at the Web site, shows how to save simple error information to a database. It saves only the records creation time and a description. A real incident management system would require much more information. When you click the programs Raise Error button, it raises a divide by zero error. The programs error handler calls subroutine SaveErrorToDatabase to create the new error record as shown in the following code.
Raise an error.
Private Sub cmdRaiseError_Click()
On Error GoTo RaiseErrorError
Raise a divide by zero error.
Err.Raise 11
RaiseErrorError:
SaveErrorToDatabase FileError.cmdRaiseError_Click
End Sub
Subroutine SaveErrorToDatabase, shown in the following code, calls the MakeErrorMessage function to generate an appropriate error message. It then opens the error database, uses an SQL statement to create the new error record, and closes the database. In this example, the program uses the Access database errors.mdb in the current directory. You could modify it to use a database in a different directory. You could also use a different kind of database, such as Oracle or Informix. If SaveErrorToDatabase encounters an error while creating the new error record, it falls back to the less-complicated strategy of displaying an error message. It uses MakeErrorMessage to create a new message describing the problem with the database and it displays both messages to the user.
Create a new error record in the error database.
Public Sub SaveErrorToDatabase(ByVal routine_name As String)
Dim txt1 As String
Dim txt2 As String
Dim error_database As Database
Dim database_open As Boolean
Dim database_name As String
Create the error message.
txt1 = MakeErrorMessage(routine_name)
On Error GoTo SaveErrorToDatabaseError
Open the database.
database_name = App.Path
If Right$(database_name, 1) <> \ _
Then database_name = database_name & \
database_name = database_name & errors.mdb
Set error_database = OpenDatabase(database_name)
Remember that the database is open.
database_open = True
Save the new information.
error_database.Execute _
INSERT INTO AutoErrors VALUES( & _
Now, & txt1 & ')
Close the database.
error_database.Close
Exit Sub
SaveErrorToDatabaseError:
There was an error saving to the file.
Just present the message.
txt2 = MakeErrorMessage(SaveErrorToFile)
MsgBox Error saving error message to database. & _
vbCrLf & _
Please record the following information & _
and send it to the development team: & _
vbCrLf & vbCrLf & txt2 & _
vbCrLf & vbCrLf & txt1
Close the database if we opened it.
If database_open Then error_database.Close
Exit Sub
End Sub
Make an appropriate error message.
Public Function MakeErrorMessage(ByVal routine_name As String) As String
MakeErrorMessage = _
Unexpected error & _
Format$(Err.Number) & _
in & routine_name & . & _
vbCrLf & Err.Description
End Function
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|